package com.bit.bit_forum.controller;

import com.bit.bit_forum.common.AppResult;
import com.bit.bit_forum.common.ResultCode;
import com.bit.bit_forum.config.AppConfig;
import com.bit.bit_forum.model.Message;
import com.bit.bit_forum.model.User;
import com.bit.bit_forum.service.IMessageService;
import com.bit.bit_forum.utils.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private IMessageService messageService;
//    发送站内信:
    @RequestMapping("/create")
    public AppResult create(HttpServletRequest req,
                            @RequestParam("receiveUserId") @NonNull Long receiveUserId,
                            @RequestParam("content") @NonNull String content){
        HttpSession session = req.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user==null || user.getDeleteStat()==1){
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        messageService.create(message);
        return AppResult.success("发送成功!");
    }
    @RequestMapping("/unReadCount")
    public AppResult getUnReadCount(HttpServletRequest req){
        HttpSession session = req.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user==null || user.getDeleteStat()==1){
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        Integer i = messageService.selectUnReadCount(user.getId());
        return AppResult.success(i);
    }
    @RequestMapping("/getMessageList")
    public AppResult getMessageList(HttpServletRequest req){
        HttpSession session = req.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if(user==null || user.getDeleteStat()==1){
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        List<Message> messages = messageService.selectMessageList(user.getId());
        return AppResult.success(messages);
    }
//    更新站内信状态为已读:
    @RequestMapping("/updateState")
    public AppResult updateStateById(@RequestParam("id") @NonNull Long id){
        Message message = messageService.selectById(id);
        if(message==null || message.getDeleteStat()==1){
            //站内信不存在:
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        messageService.updateStateById(id,(byte)1);
        return AppResult.success();
    }

    @RequestMapping("/reply")
    public AppResult reply(HttpServletRequest req,
                        @RequestParam("replyId") @NonNull Long replyId,
                       @RequestParam("content") @NonNull String content){
        HttpSession session = req.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            //检查用户状态:
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        Message message = messageService.selectById(replyId);
        if(message==null || message.getDeleteStat()==1){
            //要回复的站内信不存在
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //检查要回复的站内信是否是自己的:
        if(user.getId()==message.getPostUserId()){
            return AppResult.failed("不能给自己的私信回复");
        }
        //构造回复数据
        Message newMessage = new Message();
        newMessage.setPostUserId(message.getReceiveUserId());
        newMessage.setReceiveUserId(message.getPostUserId());
        newMessage.setContent(content);
        messageService.reply(replyId,newMessage);
        return AppResult.success();
    }
}
